#include "stdafx.h"
#include "GameMap.h"


CGameMap::CGameMap()
{
}


CGameMap::~CGameMap()
{
}

void CGameMap::CreateMapInfo(int type) {
	srand((unsigned)time(NULL));
	int b = 0, c = 0, d = 0, r;
	int camouflage_cout = 15, debris_count = 15,  barrier_count = 50;
	m_type = rand() % 3;
	for (int i = 0; i < MAP_ARRAY_Y_COUNT; i++) {
		for (int j = 0; j < MAP_ARRAY_X_COUNT; j++) {
			r = rand() % 10 + 1;
			if (r == 1 && b < barrier_count) {
				m_MapInfo[i][j] = BARRIER;
				b++;
			}
			else if (r == 2 && c < camouflage_cout) {
				m_MapInfo[i][j] = CAMOUFLAGE;
				c++;
			}
			else if (d == 4 || d == 5 && d < debris_count) {
				m_MapInfo[i][j] = DEBRIS;
				d++;
			}
			else m_MapInfo[i][j] = PASSABLE;
		}
	}
	for (int i = 0; i < MAP_ARRAY_Y_COUNT; i++) {
		for (int j = MAP_ARRAY_X_COUNT / 2 - 3; j < MAP_ARRAY_X_COUNT / 2 + 3; j++) {
			r = rand() % 5;
			if (m_type != 0 && r == 1)
				m_MapInfo[i][j] = TRAIL;
			else
				m_MapInfo[i][j] = PASSABLE;
		}
	}
}

void CGameMap::SetImage(IplImage *img[MAP_TYPE_COUNT][MAP_ELEMENT_IMAGE_COUNT]) {
	for (int j = 0; j < MAP_TYPE_COUNT; j++) {
		for (int i = 0; i < MAP_ELEMENT_IMAGE_COUNT; i++) {
			if (img[j][i] == NULL)
				break;
			m_MapElement[j][i] = img[j][i];
		}
	}
}

void CGameMap::DrawBackImage(IplImage *Bg) {
	for (int i = 0; i < MAP_ARRAY_Y_COUNT; i++) {
		for (int j = 0; j < MAP_ARRAY_X_COUNT; j++) {
			int keyb = 255, keyr = 255, keyg = 255;
			CTools::DrawToBackground(j * GRID_SIZE, i * GRID_SIZE, Bg, m_MapElement[m_type][PASSABLE], keyb, keyg, keyr);
			CTools::DrawToBackground(j * GRID_SIZE, i * GRID_SIZE, Bg, m_MapElement[m_type][m_MapInfo[i][j]], keyb, keyg, keyr);
		}
	}
}

CMapNeighbor CGameMap::GetNeighbor(CvPoint pos) {
	CMapNeighbor n;

	if (pos.y - 1 < 0 || m_MapInfo[pos.y - 1][pos.x] == BARRIER)	n.m_Up = false;
	else n.m_Up = true;
	if (pos.y + 1 >= MAP_ARRAY_Y_COUNT || m_MapInfo[pos.y + 1][pos.x] == BARRIER)	n.m_Down = false;
	else n.m_Down = true;
	if (pos.x - 1 < 0 || m_MapInfo[pos.y][pos.x - 1] == BARRIER)	n.m_Left = false;
	else n.m_Left = true;
	if (pos.x + 1 >= MAP_ARRAY_X_COUNT || m_MapInfo[pos.y][pos.x + 1] == BARRIER)	n.m_Right = false;
	else n.m_Right = true;

	return n;
}

bool CGameMap::CheckisBarrier(CvPoint pos)
{
	if (m_MapInfo[pos.x][pos.y] == BARRIER)	return true;
	else return false;
}
